#'
#' Generate a randomly distributed community with given species abundance distribution
#'
#' @param splist a vector of species names
#' @param abundance a vector of species abundance
#' @param plotdim the ranges of the plot
#' @param traits a dataframe of traits
#'
#'@return
#'A community object
#'
#'@examples
#' set.seed(1)
#' splist=paste("sp",1:4,sep="")
#' abundance=sample(1:40,4)
#' plotdim=c(80,100)
#' com=rcommunity(splist,abundance,plotdim)
#' com
#' plot(com)
#'
#' @export
rcommunity=function(splist=c("sp1","sp2"),abundance=c(10,20),plotdim=c(100,100),traits=NULL){
  if(length(abundance)!=length(splist)){
    stop("Length of species abundance vector does not equal to the total richness!")
  }
  
  species=unlist(rep(splist,abundance))
  x=as.numeric(unlist(sapply(abundance,runif,max=plotdim[1])))
  y=as.numeric(unlist(sapply(abundance,runif,max=plotdim[2])))
  if(is.null(traits)){
    com=community(species=species,x=x,y=y,plotdim=plotdim)
  }else{
    com=community(species=species,x=x,y=y,plotdim=plotdim,traits = traits)
  }
  return(com)
  
}